AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:Initialize()
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_PROJECTILE)
	self.Entity:DrawShadow(false)
	self.Entity:SetGravity(1)
	
	self.Entity.Exists = true

	local physobj = self.Entity:GetPhysicsObject()
	physobj:SetDamping(0, 0)
	--physobj:SetMass(1)
	physobj:EnableDrag(false)
	physobj:EnableGravity(true)
	
	self.Entity.CDamage = self.Entity.CDamage or {}
	self.Entity.CDamage.type = "projectile"
	
	self.Entity:SetNetworkedBool("Drawmodel", true)
	self.Entity:NextThink(CurTime() + 0.1)
end

function ENT:PhysicsCollide(data, physobj)
	self.Entity:GetPhysicsObject():SetVelocity(Vector(0,0,0))
	if util.tobool(self.Entity.BlastRadius) then
		timer.Simple(0.01, CDamage_blastdamage, self.Entity:GetPos(), self.Entity.BlastRadius, self.Entity.Damage, nil, self.Entity.DamageType)
	end
	timer.Simple(0.01, CDamage_damageent, data.HitEntity, self.Entity.Damage, nil, self.Entity.DamageType)
	self.Entity:DrawImpact(data.HitEntity, -1 * data.HitNormal)
	self.Entity:Desolidify()
end

function ENT:ImpactShield(shield)
	if (self.Entity.Damage > shield:GetHP()) then
		self.Entity.Damage = self.Entity.Damage - shield:GetHP()
		CDamage_damageent(shield, shield:GetHP(), nil, self.Entity.DamageType)
		self.Entity:DrawImpact(shield, CDamage_shieldsurfacenormal(shield, self.Entity:GetPos()))
	else
		CDamage_damageent(shield, self.Entity.Damage, nil, self.Entity.DamageType)
		self.Entity:DrawImpact(shield, CDamage_shieldsurfacenormal(shield, self.Entity:GetPos()))
		self.Entity:Desolidify()
	end
end

function ENT:PhysicsUpdate(phys)
	if util.tobool(self.Entity:WaterLevel()) and self.Entity.Exists then
		self.Entity.Exists = false
		self.Entity:DrawImpact(nil, Vector(0, 0, 1))
		self.Entity:Desolidify()
	end
	--phys:EnableGravity(false)
end

function ENT:Desolidify()
	self.Entity.Exists = false
	self.Entity:SetMoveType(MOVETYPE_NONE)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
	self.Entity:SetNotSolid(true)
	self.Entity:SetNetworkedBool("Drawmodel", false)
	self.Entity.CDamage.type = "immune"
	self.Entity:GetPhysicsObject():EnableMotion(false)
	timer.Simple(3, function() if self.Entity:IsValid() then self.Entity:Remove() end end)
end